/*
 * Copyright 1997-2011
 *
 * http://www.pconline.com.cn
 *
 */
package cn.pconline.autoclub.util.ubb.node;

import cn.pconline.autoclub.util.ubb.EscapeUtils;
import cn.pconline.autoclub.util.ubb.Node;

/**
 *
 * @author xhchen
 */
public class ImgNodeMobile extends AbstractNode {
	public ImgNodeMobile() {}

	private ImgNodeMobile(String name, String attribute) {
		super.name = name;
		super.attribute = attribute;
	}

	@Override
	public StringBuilder getText(java.util.LinkedList<Node> stack) {
		StringBuilder sb = new StringBuilder();
        String muri = makeMobileUri(text.toString());
        if (muri == null) {
            sb.append("<div class=\"ext_img\" title=\"");
            sb.append(text);
            sb.append("\" onclick=\"dex(this);\">站外图片");
            sb.append("</div>");
        } else {
            sb.append("<img border=\"0\"");
            if (muri.endsWith(".gif") || muri.endsWith(".GIF")) {
                // do nothing
            } else {
                sb.append(" class=\"aw\"");
            }
            sb.append(" onclick=\"viewPic(this, event);\" ");
            sb.append(" src=\"").append(muri).append("\"/>");
        }

		return sb;
	}

    static String makeMobileUri(String uri) {
    	 //汽车网和电脑网回传缩略图
//        boolean isAuto = SystemConstant.AUTO_APP_NAME.equals(EnvUtils.getEnv().getAppName());
//        boolean isIt = SystemConstant.ITBBS_APP_NAME.equals(EnvUtils.getEnv().getAppName());

//        if(isAuto){
//        	uri = uri.replace(SystemConstant.PICTURE_SUBFIX, "_120x80");
//        }
//        if(isIt){
//        	uri = uri.replace(SystemConstant.PICTURE_SUBFIX_IT, "_120x90");
//        	uri = uri.replace(SystemConstant.PICTURE_SUBFIX, "_120x90");
//        }
//        if(uri.contains(SystemConstant.PICTURE_SUBFIX_IT)){
//            uri = uri.replace(SystemConstant.PICTURE_SUBFIX_IT, "_120x90");
//        }
//        if(uri.contains(SystemConstant.PICTURE_SUBFIX)){
//            uri = uri.replace(SystemConstant.PICTURE_SUBFIX, "_120x80");
//        }
        if (uri.startsWith("http://img.pconline.com.cn/")
                ||uri.startsWith("http://img.pcauto.com.cn/")
                ||uri.startsWith("http://img.pcgames.com.cn/")
                ||uri.startsWith("http://img.pclady.com.cn/")
                ||uri.startsWith("http://img.pcbaby.com.cn/")
                ||uri.startsWith("http://img.pchouse.com.cn/")
                ) {
            return "http://imgm" + uri.substring(10);
        } else {
        	int pos = uri.indexOf('.');
            String tmps = uri.substring(pos + 1);
            if (tmps.startsWith("pconline.com.cn")
                    || tmps.startsWith("pcauto.com.cn")
                    || tmps.startsWith("pcgames.com.cn")
                    || tmps.startsWith("pclady.com.cn")
                    || tmps.startsWith("pcbaby.com.cn")
                    || tmps.startsWith("pchouse.com.cn")
                    || tmps.startsWith("3conline.com")
                    ) {
                return uri;
            }
            return null;
        }

    }

	@Override
	public void appendText(CharSequence text) {
		if (attribute == null) {
			EscapeUtils.appendAttribute(text, super.text);
		} else {
			EscapeUtils.appendHtml(text, super.text);
		}
	}

	@Override
	public Node copyWithoutText() {
		return new ImgNodeMobile(name, attribute);
	}

	@Override
	public Node build(String name, String attribute) {
		return new ImgNodeMobile(name, attribute);
	}
}
